package com.ruoyi.newSystem.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.PageResult;
import com.ruoyi.common.utils.BaseMapperX;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.newSystem.bean.input.UserPageReqVO;
import com.ruoyi.common.core.domain.entity.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

/**
 * The interface Admin user mapper.
 */
@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {

    /**
     * Select by username admin user do.
     *
     * @param username the username
     * @return the admin user do
     */
    default AdminUserDO selectByUsername(String username) {
        return selectOne(AdminUserDO::getUsername, username);
    }


    /**
     * Select page page result.
     *
     * @param reqVO   the req vo
     * @param deptIds the dept ids
     * @return the page result
     */
    default PageResult<AdminUserDO> selectPage(UserPageReqVO reqVO, Collection<Long> deptIds) {
        PageResult<AdminUserDO> adminUserDOPageResult = new PageResult<>();
        LambdaQueryWrapper<AdminUserDO> objectLambdaQueryWrapper = new LambdaQueryWrapper<AdminUserDO>();
        objectLambdaQueryWrapper.like(StringUtils.isNotBlank(reqVO.getUsername()), AdminUserDO::getUsername, reqVO.getUsername());

        //count
        Long count = selectCount(objectLambdaQueryWrapper);
        adminUserDOPageResult.setTotal(count);
        //list
        objectLambdaQueryWrapper.orderByDesc(AdminUserDO::getCreateTime);
        objectLambdaQueryWrapper.last(StringUtils.format(" LIMIT {} OFFSET (({} - 1) * {})", reqVO.getPageSize(), reqVO.getPageNo(), reqVO.getPageSize()));

        List<AdminUserDO> adminUserDOS = selectList(objectLambdaQueryWrapper);
        adminUserDOPageResult.setList(adminUserDOS);
        return adminUserDOPageResult;
    }



}
